/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SerialPortsWindLines.java
 *
 * Created on 2010-7-29, 19:13:00
 */

package com.hitangjun.desk;

import gnu.io.SerialPort;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.*;

import static com.hitangjun.rel.TimerControl.*;


/**
 * 主页页面外观
 * @author zjy
 */
public class SerialPortsMainView{

	 SerialPortReader sr =new SerialPortReader();
	 private String port="";//端口号
	 private String rate="";//波特率
    private String dataBit = "";//数据位
    private String stopBit = "";//停止位
    private String parity ="NONE";//奇偶校验标记
    /** Creates new form SerialPortsWindLines */
    public SerialPortsMainView() {
        //设置窗体外观为windows
    	try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(Exception e){
            Log.debug("设置窗体windows外观失败");
        }
        initView();
        //打开串口
        openSerialPort();
    }

    /**
     * 显示页面方法
     */
    private void initView(){
        JFrame frame=new JFrame("HQ读写器软件");
        JPanel p1 = new JPanel();    //创建面板1
        p1.setLayout(null);//设置绝对布局
        //下拉框
        JLabel selectLabel = new JLabel("选择烧写器");
        JComboBox comboBox = new JComboBox();//下拉选择框
        String[] select = {"烧写器1","烧写器2","烧写器3"};
        comboBox.setModel(new DefaultComboBoxModel(select));
        selectLabel.setBounds(100, 15, 100, 25);
        comboBox.setBounds(400, 15, 100, 25);
        p1.add(selectLabel);
        p1.add(comboBox);
        //注册按钮
        JButton registBt = new JButton("注册");
        registBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                sendRegistDataBtnEvent(evt);
            }
        });
        //登录按钮
        JButton loginBt=new JButton("登录");
        loginBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                sendLoginDataBtnEvent(evt);
            }
        });

        JButton downloadBt=new JButton("下载程序");
        JButton resetBt=new JButton("重置烧写器");
        registBt.setBounds(100,80,120,40);
        loginBt.setBounds(250, 80, 120, 40);
        downloadBt.setBounds(400, 80, 120, 40);
        resetBt.setBounds(550, 80, 120, 40);

        p1.add(registBt);
        p1.add(loginBt);   //像页面中添加该按钮
        p1.add(downloadBt);   //像页面中添加该按
        p1.add(resetBt);   //像页面中添加该按钮

        JLabel surplusLabel = new JLabel("剩余烧写次数");
        int count = 3;
        JLabel surplusLabel2 = new JLabel("count");
        surplusLabel.setBounds(100, 140, 150, 40);
        surplusLabel2.setBounds(400, 140, 150, 40);

        p1.add(surplusLabel);
        p1.add(surplusLabel2);

        //三个复选框
        JCheckBox cbRead = new JCheckBox();
        JLabel readLabel = new JLabel("读配置");

        JCheckBox cbWrite1 = new JCheckBox();
        JLabel readInfoLabel = new JLabel("读信息");

        JCheckBox cbWrite2 = new JCheckBox();
        JLabel writeLabel = new JLabel("写配置");

        JButton beginBt=new JButton("开始");
        beginBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                sendBeginDataBtnEvent(evt);
            }
        });

        cbRead.setBounds(100, 180, 20, 20);
        readLabel.setBounds(130, 180, 200, 20);

        cbWrite1.setBounds(100, 210, 20, 20);
        readInfoLabel.setBounds(130, 210, 200, 20);

        cbWrite2.setBounds(100, 240, 20, 20);
        writeLabel.setBounds(130, 240, 200, 20);

        beginBt.setBounds(120, 270, 120, 30);

        //将三个复选框添加到面板1
        p1.add(cbRead);
        p1.add(readLabel);
        p1.add(cbWrite1);
        p1.add(readInfoLabel);
        p1.add(cbWrite2);
        p1.add(writeLabel);
        p1.add(beginBt);

        //实例化文本框
        JTextArea jta= new JTextArea();
        //放入滑动窗口
        JScrollPane jsp = new JScrollPane(jta);
        jta.setLineWrap(true);
        jsp.setBounds(100,350,600,200);
        p1.add(jsp);
        frame.add(p1);
        frame.setBounds(300,200,800,600);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    //转换预设的奇偶校验位的值
    private int getParityValue(String parity){
    	 if("NONE".equals(parity)){
      	   return SerialPort.PARITY_NONE;
         }else if("ODD".equals(parity)){
        	 return SerialPort.PARITY_ODD;
         }else if("EVEN".equals(parity)){
        	 return SerialPort.PARITY_EVEN;
         }else if("MARK".equals(parity)){
        	 return SerialPort.PARITY_MARK;
         }else if("SPACE".equals(parity)){
        	 return SerialPort.PARITY_SPACE;
         }else{
        	 return SerialPort.PARITY_NONE;
         }
    }

    /**
     * 读取参数值,打开串口
     */
    public void openSerialPort() {
        //0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x00 0XFF 0x11 0x12 0x13
        HashMap<String, Comparable> params = new HashMap<String, Comparable>();

        int parityInt = getParityValue(parity);
        String paramsMsg = "打开串口 "+port+","+rate+","+parity+","+dataBit+","+stopBit;

        params.put( SerialPortReader.PARAMS_PORT, port ); // 端口名称
        params.put( SerialPortReader.PARAMS_RATE, rate ); // 波特率
        params.put( SerialPortReader.PARAMS_DATABITS,dataBit  ); // 数据位
        params.put( SerialPortReader.PARAMS_STOPBITS, stopBit ); // 停止位
        params.put( SerialPortReader.PARAMS_PARITY, parityInt ); // 无奇偶校验
        params.put( SerialPortReader.PARAMS_TIMEOUT, 1000 ); // 设备超时时间 1秒
        params.put( SerialPortReader.PARAMS_DELAY, 200 ); // 端口数据准备时间 1秒
        try {
			sr.open(params);
			sr.addObserver( new Observer() {

                public void update(Observable o, Object arg) {
					// TODO Auto-generated method stub
					  //根据收到的数据
				    	Log.debug("读取到的数据位数："+(( byte[] ) arg).length);
				    	String v= new String( ( byte[] ) arg );
				}
			} );
			successOpenPort(paramsMsg);
		} catch (SerialPortException e) {
            System.out.printf(e.getMessage());
		}
    }


    /**
     * 设置打开端口失败的信息
     * @param text
     */
    void successOpenPort(String text){
    	Log.debug(text);
        System.out.printf("就绪");
    }

    private void openOrCloseToggleButtonMouseClicked(java.awt.event.MouseEvent evt) {
    	//如果没有打开则打开端口否则关闭端口
    	if(!sr.isOpen()){
//        	setToggleBtnOpen();
	         openSerialPort();
        }else{
        	try {
        		sr.close();
        	} catch (SerialPortException e) {
        		System.out.println (e.getMessage());
        	}
        }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SerialPortsMainView();
            }
        });
    }

    int lastIndex = 0;

    /**
     * 将16进制字符串转换成int
     */
    public int[] hexStrToInt(String hexStr){
        String[] hexStrArray = hexStr.split( " " );
        int[] result;
		try {
			result = new int[hexStrArray.length];
			for(int i=0;i<hexStrArray.length;i++){
			    result[i] = Integer.parseInt( hexStrArray[i].substring( 2 ), 16 );
			}
			return result;
		} catch (NumberFormatException e) {
            System.out.printf("输入的数据不正确");
		}
		return null;
    }



    /**
     * 点击注册按钮鼠标事件
     * @param evt
     */
    private void sendRegistDataBtnEvent(ActionEvent evt)
    {
        //调用注册定时器 循环发送请求
        timingRegistRequset();
    }

    /**
     * 发送登录按钮鼠标事件
     * @param evt
     */
    private void sendLoginDataBtnEvent(ActionEvent evt)
    {
        //调用登录定时器 循环发送请求
        timingLoginRequset();
    }

    /**
     * 发送开始按钮鼠标事件
     * @param evt
     */
    private void sendBeginDataBtnEvent(ActionEvent evt)
    {
        //调用登录定时器 循环发送请求
        timingBeginRequset();
    }


}
